package com.cz.influxdb.test;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.cz.influxdb.config.InfluxDBHelper;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.domain.WritePrecision;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class InsertWeather {
    public static void main(String[] args) {
        InfluxDBHelper helper = new InfluxDBHelper();
        InfluxDBClient client = helper.getInfluxDBClient();


        String date = "1900-01-01 00:00:00";
        for (int i = 1; i <  65744640; i++) {
            WeatherEntity weatherEntity = new WeatherEntity();
            weatherEntity.setLocation("上海");
            Date beginDate = DateUtil.parse(date);
            Date curDate = DateUtil.offsetMinute(beginDate,i);
            weatherEntity.setDate(DateUtil.format(curDate, DatePattern.NORM_DATETIME_PATTERN));
            weatherEntity.setTemperature(RandomUtil.randomDouble(50));
            weatherEntity.setHumidity(RandomUtil.randomDouble(100));
            weatherEntity.setTime(Instant.ofEpochSecond(curDate.getTime()/1000));
            client.getWriteApiBlocking().writeMeasurement(WritePrecision.NS, weatherEntity);
            if (i % 10000 == 0) {
                System.out.println("已插入" + i + "条数据");
            }
        }
        client.close();
    }
}
